// pages/chongzhi/chongzhi.js
const app = getApp();
var api = require("../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 0
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "money") {
      that.setData({
        money: inputVlaue
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  sendRequest: function () {

    var that = this;
    if (!that.data.money) {
      wx.showToast({
        title: '请输入金额',
        icon: 'none'
      })
      return false;
    }
    var money = that.data.money;
    app.request({
      url: api.user.recharge,
      method: 'post',
      data: {
        price: money
      },
      success: function (e) {
        console.log(e)
        if (e.code == 1) {
          var pay = e.data;
          wx.requestPayment({
            timeStamp: pay.timeStamp,
            nonceStr: pay.nonceStr,
            package: pay.package,
            signType: pay.signType,
            paySign: pay.paySign,
            success: function (res) {
              wx.showToast({
                title: '充值成功',
                duration: 1000,
                success: function () {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 1000)
                }
              })
            },
            fail: function () {
              wx.showToast({
                title: '支付失败',
                icon: 'none',
                mask: true
              })
            }
          })
        } else {
          wx.showToast({
            title: '支付失败',
            icon: 'none',
            mask: true
          })
        }
      }
    })
  }
})